<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">      

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标题" prop="title" class="person">
            <el-input
              v-model="form.title"
              placeholder="标题"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="type" class="person">
            <el-radio-group v-model="form.type">
              <el-radio :label="'0'">指南</el-radio>
              <el-radio :label="'1'">专家</el-radio>
              <el-radio :label="'2'">资讯</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="图标" prop="icon" class="person">
            <FileUpload
              v-model="form.icon"
              :limit="1"
              :isMultiple="false"
              :fileType="['jpg', 'png']"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="焦点图" prop="thumb" class="person">
            <FileUpload
              v-model="form.thumb"
              :limit="1"
              :isMultiple="false"
              :fileType="['jpg', 'png']"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="发布者" prop="createUser" class="person">
            <el-input
              v-model="form.createUser"
              placeholder="发布者"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否置顶" prop="isTop" class="person">
            <el-radio-group v-model="form.isTop">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="agent" prop="model">
            <el-autocomplete
              style="width: 100%"
              v-model="form.model"
              :fetch-suggestions="queryModelAsync"
              placeholder="绑定Agent"
              @select="handleSelectModel"
            ></el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sortNo">
            <el-input
              type="number"
              v-model="form.sortNo"
              placeholder="排序"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import BindDialog from '@/components/bindDialog/index.vue'
import { dialogFrame } from './frameData'
import { modifyAssistant, addAssistant } from '@/api/assistant'
import { getGuideList, getGuideById } from '@/api/guide';
 

const markForm = () => {
  return {
    title: "",
    type: "0",
    icon: "",
    thumb: "",
    clickNum: 0,
    isTop: 0,
    modelId: '',
    model: '',
    createUser: '',
    sortNo: '-1'
  }
}

export default {
  name: "",
  components: {
    BindDialog
  },
  data() {
    return {
      rules: {
        title: [{ required: true, message: '标题是必填项', trigger: 'blur' }],
        type: [{ required: true, message: '类型是必填项', trigger: 'change' }],
        modelId: [{ required: true, message: '必填项', trigger: 'change' }],
      },
      title: "",
      type: "",
      config: {
        edit: {
          func: modifyAssistant,
          title: "编辑"
        },
        new: {
          func: addAssistant,
          title: "新增"
        }
      },
      form: markForm(),
      visible: false,
      inputValue: "",
      inputVisible: false,
      dialogFrame,
      query: '',
      loading: false,
      modelList: [],
    };
  },

  methods: {
    queryModelAsync(name, cb) {
      getGuideList({
        pageNum: 1,
        pageSize: 200
      }).then(res => {
        const rows = res.rows.map(item => {
          return {
            ...item,
            value: item.name
          }
        })
        cb(rows);
      })
    },

    getGuide(id){
      getGuideById(id).then(res => {
        this.form.model = res?.data?.name
        this.form.modelId = res?.data?.id
      })
    },
    
    handleSelectModel(item) {
      this.form.modelId = item.id
      console.log(item);
    },
    handleClose(tag) {
      this.form.problemList.splice(this.form.problemList.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput?.$refs?.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.form.problemList.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },

    showDialog(row) {
      this.reset();
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      if (isEdit) {
        this.getGuide(row.modelId)
        this.form = Object.assign(this.form, row);
      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const data = { ...this.form }

          if(this.type === "new"){
            delete data.id;
          }

          data.routineProblem = JSON.stringify(data.problemList)
          delete data.problemList;
          delete data.model
          delete data.modelVo
          delete data.createTime

          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("getTableList");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
      this.fileList = [];
    },

    // 表单重置
    reset() {
      this.form = markForm();
      this.$forceUpdate();
      this.fileList = [];
    },

  },
};
</script>
<style scoped lang="scss">
.label-list{
  height: 36px;
  display: flex;
  align-items: center;

  .el-tag{
    margin-right: 10px;
  }

  .button-new-tag{
    height: 28px;
    padding: 0 20px;
  }

  .input-new-tag{
    width: 100px;
    height: 28px;
    display: flex;

    ::v-deep .el-input__inner {
      width: 100%;
      height: 100%;
    }
  }
}
.person{
  position: relative;

  .el-icon-user{
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }

  .cover{
    width: 100%;
    height: 100%;
    position:absolute;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: pointer;

    &:hover {
      .el-input__inner{
        border-color: #C0C4CC;
      }
    }
  }
}
</style>
